package com.ship.boats.business;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ship.boats.domain.SpBoatsReceivingStation;
import com.ship.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>UploadPortFileBusiness</b>
 *
 * @description: UploadPortFileBusiness <br>
 * @date: 2024/4/28 16:06 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Component
public class UploadPortFileBusiness {
    private static final Logger logger = LoggerFactory.getLogger(UploadPortFileBusiness.class);

    public List<SpBoatsReceivingStation> uploadList(MultipartFile file) throws Exception {
        List<SpBoatsReceivingStation> receivingStationList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(ins);
            List<Map<String, Object>> maps = reader.readAll();
            // 从第三行开始读取
            AtomicInteger i = new AtomicInteger();
            maps.forEach(column -> {
                SpBoatsReceivingStation spBoatsReceivingStation = new SpBoatsReceivingStation();
                if (!String.valueOf(column.get("Jetties")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Jetties")))) {
                    spBoatsReceivingStation.setJetties(Integer.valueOf(String.valueOf(column.get("Jetties"))));
                }
                if (!String.valueOf(column.get("Tanks")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Tanks")))) {
                    spBoatsReceivingStation.setTanks(Integer.valueOf(String.valueOf(column.get("Tanks"))));
                }
                if (!String.valueOf(column.get("Long")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Long")))) {
                    spBoatsReceivingStation.setLon(String.valueOf(column.get("Long")));
                }
                if (!String.valueOf(column.get("Lat")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Name")))) {
                    spBoatsReceivingStation.setLat(String.valueOf(column.get("Lat")));
                }
                if (!String.valueOf(column.get("Startup")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Startup")))) {
                    spBoatsReceivingStation.setStartup(String.valueOf(column.get("Startup")));
                }
                if (!String.valueOf(column.get("Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Status")))) {
                    spBoatsReceivingStation.setStatus(String.valueOf(column.get("Status")));
                }
                if (!String.valueOf(column.get("Capacity")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Capacity")))) {
                    spBoatsReceivingStation.setCapacity(new BigDecimal(String.valueOf(column.get("Capacity"))));
                }
                if (!String.valueOf(column.get("Storage")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Storage")))) {
                    spBoatsReceivingStation.setStorage(Integer.valueOf(String.valueOf(column.get("Storage"))));
                }
                if (!String.valueOf(column.get("Type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Type")))) {
                    spBoatsReceivingStation.setType(String.valueOf(column.get("Type")));
                }
                if (!String.valueOf(column.get("Zone")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Zone")))) {
                    spBoatsReceivingStation.setZone(String.valueOf(column.get("Zone")));
                }
                if (!String.valueOf(column.get("Country")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Country")))) {
                    spBoatsReceivingStation.setCountry(String.valueOf(column.get("Country")));
                }
                if (!String.valueOf(column.get("Port")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Port")))) {
                    spBoatsReceivingStation.setPort(String.valueOf(column.get("Port")));
                }
                if (!String.valueOf(column.get("SHAREHOLDERS")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("SHAREHOLDERS")))) {
                    spBoatsReceivingStation.setShareholders(String.valueOf(column.get("SHAREHOLDERS")));
                }
                if (!String.valueOf(column.get("CAPACITY HOLDERS")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("CAPACITY HOLDERS")))) {
                    spBoatsReceivingStation.setCapacityHolders(String.valueOf(column.get("CAPACITY HOLDERS")));
                }
                if (!String.valueOf(column.get("Operato")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Operato")))) {
                    spBoatsReceivingStation.setOperators(String.valueOf(column.get("Operato")));
                }
                if (!String.valueOf(column.get("Buyers")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Buyers")))) {
                    spBoatsReceivingStation.setBuyers(String.valueOf(column.get("Buyers")));
                }
                if (!String.valueOf(column.get("Suppliers")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Suppliers")))) {
                    spBoatsReceivingStation.setSuppliers(String.valueOf(column.get("Suppliers")));
                }
                if (!String.valueOf(column.get("Address")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Address")))) {
                    spBoatsReceivingStation.setAddress(String.valueOf(column.get("Address")));
                }
                if (!String.valueOf(column.get("Process")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Process")))) {
                    spBoatsReceivingStation.setProcess(String.valueOf(column.get("Process")));
                }
                if (!String.valueOf(column.get("Website")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Website")))) {
                    spBoatsReceivingStation.setWebsite(String.valueOf(column.get("Website")));
                }
                if (!String.valueOf(column.get("Max ship size")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Max ship size")))) {
                    spBoatsReceivingStation.setMaxShipSize(String.valueOf(column.get("Max ship size")));
                }
                if (!String.valueOf(column.get("Cost")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Cost")))) {
                    spBoatsReceivingStation.setCost(String.valueOf(column.get("Cost")));
                }
                if (!String.valueOf(column.get("Name")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Name")))) {
                    spBoatsReceivingStation.setName(String.valueOf(column.get("Name")));
                    receivingStationList.add(spBoatsReceivingStation);
                }
            });
        } catch (IOException e) {

            logger.error("上传数据异常,文件解析:{}", e.getMessage());
        } catch (Exception e) {

            logger.error("上传数据异常,程序: {}", e.getMessage());
        }
        return receivingStationList;
    }
}
